import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:pet_app/base/base_controller.dart';
import 'package:pet_app/base/base_list_controller.dart';
import 'package:pet_app/model/breed_item.dart';
import 'package:pet_app/model/enums/pet_type.dart';
import 'package:pet_app/model/query/breed_search_query.dart';
import 'package:pet_app/net/http.dart';
import 'package:pet_app/routes/router.dart';
import 'package:pet_app/widget/loading_widget.dart';

class BreedSearchLogic extends BaseListController {
  List<BreedItem> breedItemList = [];
  late PetType petType;
  final TextEditingController textEditingController = TextEditingController();
  String name = '';

  @override
  void initData() {
    petType = Get.arguments['petType'];
    getNetworkData(configNetworkParams());
  }

  @override
  dynamic configNetworkParams() {
    return BreedSearchQuery(petType.value,name: name);
  }

  @override
  void getNetworkData(dynamic params) {
    super.getNetworkData(params);
    Loading.show();
    Http().client.breedList(params).then((resp) {
      netState = NetState.dataSussessState;
      Loading.dismiss();
      List<BreedItem> list = resp.data?.list ?? [];
      if (page == 1) {
        breedItemList = list;
        refreshController.refreshCompleted();
      } else {
        breedItemList.addAll(list);
        refreshController.loadComplete();
      }
      if (breedItemList.isEmpty) {
        netState = NetState.emptyDataState;
      }
      if (list.length < 10) {
        refreshController.loadNoData();
      }
      update();
    });
  }

  @override
  void loadMore() {
    super.loadMore();
    page += 1;
    getNetworkData(configNetworkParams());
  }

  @override
  void refreshData() {
    super.refreshData();
    page = 1;
    getNetworkData(configNetworkParams());
  }

  void toArticlePage(num breedId) {
    Get.toNamed(AppRoutes.articleDetail,
        arguments: {'breedId': breedId});
  }

  void onChanged(String value) {
    name = value;
    update(['searchBtn']);
  }

  void search() {
    page = 1;
    getNetworkData(configNetworkParams());
  }
}
